package cn.funnymap.lgis.utils;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author jiaoxn
 */
public class UrlUtil {
    private UrlUtil() {}

    /**
     * 去掉URL中的查询参数并返回新的URL
     *
     * @param url 需要处理的URL
     * @return 不带查询参数的新的URL
     * @throws MalformedURLException 异常内容
     */
    public static URL splitQueryParameters(URL url) throws MalformedURLException {
        if (url.getQuery() == null) {
            return url;
        }

        String serviceUrlWithoutQuery;
        if (url.getPort() == -1) {
            serviceUrlWithoutQuery = String.format("%s://%s%s",
                    url.getProtocol(),
                    url.getHost(),
                    url.getPath());
        } else {
            serviceUrlWithoutQuery = String.format("%s://%s:%d%s",
                    url.getProtocol(),
                    url.getHost(),
                    url.getPort(),
                    url.getPath());
        }

        return new URL(serviceUrlWithoutQuery);
    }
}
